﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Sharp.Infrastructure;
using Sharp.Infrastructure.WebApiClient;

namespace Sharp.WebBooter
{
    /// <summary>
    /// 权限过滤器
    /// </summary>
    public class AuthorizationFilter : IActionFilter
    {
        public void OnActionExecuting(HttpRequestMessage requestMessage)
        {
            var username = "admin";
            var id = 1;

            requestMessage.Headers.Add("appid", "platformSharp");
            if (username.IsNotNullOrEmpty())
            {
                requestMessage.Headers.Add("currentUser", username);

                var bytes = Encoding.UTF8.GetBytes(username + ":" + id);
                var authValue = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(bytes));
                requestMessage.Headers.Authorization = authValue;
            }

        }

        public Task OnActionExecuted(HttpResponseMessage responseMessage)
        {
            return Task.FromResult(0);
        }

    }
}